 
//【封装网络请求】
// get和delete请求的参数，微信那边会自动拼接到你路径，即以路径传参的形式发送后端
// post和put请求的参数，微信那边会自动放到请求体中，即以对象的形式发送给后端
class WxRequest {
  defaults = {
    baseURL: '',
    url: '',
    data: null,
    method: 'GET',
    header: {
      'Content-type': 'application/json'
    },
    timeout: 60000
  }

  constructor(params = {}) {
    this.defaults = Object.assign({}, this.defaults, params)
  }
  request (options) {
    options.url = this.defaults.baseURL + options.url
    options = {...this.defaults, ...options}
    return new Promise((resolve, reject) => {
      wx.request({
        ...options,
        success: (res) => {
          // 只要服务器响应，那么不管状态码是多少，都会走到success，所以这里可以根据业务状态码做处理，不处理也行
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  }

  get (url, data = {}, config = {}) {
    return this.request(Object.assign({ url, data, method: 'GET' }, config))
  }
  post (url, data = {}, config = {}) {
    return this.request(Object.assign({ url, data, method: 'POST' }, config))
  }
  put (url, data = {}, config = {}) {
    return this.request(Object.assign({ url, data, method: 'PUT' }, config))
  }
  delete (url, data = {}, config = {}) {
    return this.request(Object.assign({ url, data, method: 'DELETE' }, config))
  }
}

export const _instance = new WxRequest( {
  baseURL: 'https://huanf.top:7447',
  timeout: 60000
})
